from tortoise.contrib.fastapi import register_tortoise
from fastapi import FastAPI

TORTOISE_ORM = {
    "connections": {
        #  SQLite 连接
        "default": {
            "engine": "tortoise.backends.sqlite",
            "credentials": {
                "file_path": "db.sqlite3",
            },
        },
    },
    "apps": {
        "models": {
            "models": ["aerich.models", "app.models.user"],  # 注册用户模型'apps.myORM.models',
            "default_connection": "default",
        }
    },
    "use_tz": False,
    "timezone": "Asia/Shanghai",
}


def init_db(app: FastAPI):
    register_tortoise(
        app,
        config=TORTOISE_ORM,
        # db_url="sqlite://db.sqlite3",  # 可替换为 mysql/postgres
        # modules={"models": ["app.models.user"]},  # 关键点
        # generate_schemas=True,  # 启动时自动创建表
        add_exception_handlers=True,  # 添加异常处理器
    )
